// $codepro.audit.disable packageNamingConvention

/** Packages */
package edu.gatech.team404;

import java.util.ArrayList;
import java.util.List;

import edu.gatech.team404.dal.AccountDAL;

/**
 * An information holder for a team.
 * @author Michael Dandy
 * @version 1.0
 */
public class Team {
	/** The unique team ID */
	private String teamID;
	
	/** The lab ID */
	private String labID;
	
	/** The name of the team */
	private String name;
	
	/** How many extra people this team can have */
	private int override;
	
	/** The list of all members in a team */
	private List<Account> members;
	
	/**
	 * Constructor
	 * @param labId the lab ID associated with this lab
	 * @param teamId the ID of this team. It may be null. If that's the case
	 * 				 then an ID will be generated.
	 * @param name the name of the team
	 */
	public Team(String labId, String teamId, String name) {
		this (labId, teamId, name, 0, new ArrayList<Account>());
	}
	
	/**
	 * Constructor
	 * @param labId the lab ID associated with this lab
	 * @param teamId the ID of this team. It may be null. If that's the case
	 * 				 then an ID will be generated.
	 * @param name the name of the team
	 * @param members the members of the team
	 */
	public Team(String labId, String teamId, String name, int override, List<Account> members) {
		if (teamId == null) {
			teamId = generateID (labId, name);
		}
		
		this.labID = labId;
		this.teamID = teamId;
		this.name = name;
		this.override = override;
		this.members = members;
	}
	
	/**
	 * Generate an ID based on the given feed.
	 * @param labID the lab ID associated with this lab
	 * @param name the feed for ID
	 * @return the team ID
	 */
	private String generateID (String labID, String name) {
		//teamID = labID + the first three initial characters + hashcode
		final String[] wordName = name.split(" ");
		String id = "";
		
		if (wordName.length == 1) {
			id = labID + "-" + wordName[0].charAt(0) + "-" + hash(name, 3);
		}
		else if (wordName.length == 2) {
			id = labID + "-" + wordName[0].charAt(0) + wordName[1].charAt(0)  + "-"
				+ hash(name, 3);
		}
		else {
			id = labID + "-" + wordName[0].charAt(0) + wordName[1].charAt(0)  + "-"
				+ wordName[2].charAt(0) + hash(name, 3);
		}
		
		if (id.length() >= 20) {
			id = id.substring(0, 20);
		}
		
		return id;
	}
	
	/**
	 * Generate a hash with the specified digit.
	 * @param feed the feed to be hashed
	 * @param digit the number of digit
	 * @return the hash code
	 */
	private int hash (String feed, int digit) {
		int hash = Math.abs(feed.hashCode());
		return hash % ((int) Math.pow(10, digit));
	}
	
	/**
	 * Add a member to the team.
	 * @param username the username of a student
	 * @return true on success or false otherwise
	 */
	public boolean addMember (String username) {
		final AccountDAL dal = new AccountDAL();
		final Account student = dal.getAccount(username);
		return addMember(student);
	}
	
	/**
	 * Add a member to the team.
	 * @param student the student to be added
	 * @return true on success or false otherwise
	 */
	public boolean addMember (Account student) {
		if (!members.contains(student)) {
			return members.add(student);
		}
		return false;
	}
	
	/**
	 * Remove a member from the team.
	 * @param username the username of a student
	 * @return true on success or false otherwise
	 */
	public boolean removeMember (String username) {
		for (Account member : members) {
			if (member.getUsername().equalsIgnoreCase(username)) {
				return members.remove(member);
			}
		}
		return false;
	}
	
	/**
	 * Check if the specified member is in the team.
	 * @param username the username of a student
	 * @return true if the specified member is in the team or false otherwise
	 */
	public boolean isMemberExist (String username) {
		for (Account member : members) {
			if (member.getUsername().equalsIgnoreCase(username)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Remove all members from this team
	 */
	public void clearMember () {
		members.clear();
	}
	
	/**
	 * Override the maximum number of people in a team.
	 * @param extra how many additional people can this team has
	 */
	public void setOverride (int extra) {
		this.override = extra;
	}
	
	/**
	 * Get the override status.
	 * @return how many additional people can this team has
	 */
	public int getOverride () {
		return this.override;
	}
	
	/**
	 * Get the ID of the team.
	 * @return the ID of the team
	 */
	public String getTeamID() {
		return teamID;
	}

	/**
	 * Set the ID of the team.
	 * @param teamID the ID of the team
	 */
	public void setTeamID(String teamID) {
		this.teamID = teamID;
	}
	
	/**
	 * Get the ID of the lab associated with this team.
	 * @return the ID of the lab
	 */
	public String getLabID() {
		return labID;
	}

	/**
	 * Set the ID of the ab associated with this team.
	 * @param labID the ID of the lab
	 */
	public void setLabID(String labID) {
		this.labID = labID;
	}

	/**
	 * Get the name of the team.
	 * @return the name of the team
	 */
	public String getName() {
		return name;
	}

	/**
	 * Set the name of the team.
	 * @param name the name of the  team
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Get the list of team member.
	 * @return the list of team member
	 */
	public List<Account> getMembers() {
		return members;
	}

	/**
	 * Set the list of team member.
	 * @param members the list of team member
	 */
	public void setMembers(List<Account> members) {
		this.members = members;
	}

	/**
	 * Print the attributes of this course.
	 * @return the attributes of this course
	 */
	@Override
	public String toString() {
		return "Team [teamID=" + teamID + ", labID=" + labID 
				+ ", name=" + name + ", members=" + members + "]";
	}
	
	
}
